import { adminRemoveTenant } from "@/common/apis";
import request from "@/common/http";
import { goTo } from "@/common/utils";
import { tenantsStatus } from "@/common/constant";
import { Button, Dialog, Toast } from "antd-mobile";
import React from "react";

class GoodsItem extends React.Component {
  constructor(props) {
    super(props);
  }

  /**
   * 修改租户
   */
  updateTenant = () => {
    goTo(`/tenant/edit?tenantCode=${this.props.tenantItem.tenantCode}`);
  };

  /**
   * 跳转用户管理
   */
  toUserList = () => {
    goTo(`/user/list?tenantCode=${this.props.tenantItem.tenantCode}`);
  };

  /**
   * 删除租户
   */
  removeTenant = async () => {
    const result = await Dialog.confirm({
      content: "确定删除租户吗？",
    });
    if (!result) {
      return;
    }

    const { success, message } = await request.post(adminRemoveTenant, {
      tenantCode: this.props.tenantItem.tenantCode,
    });

    if (!success) {
      Toast.show({
        icon: "fail",
        content: message || "删除失败",
      });
      return;
    }

    Toast.show({
      icon: "success",
      content: "删除成功",
    });
    this.props.getTenantList();
  };

  render() {
    const { tenantItem, stopPropagation } = this.props;
    const isTenantNormal = tenantItem.tenantStatus === tenantsStatus.NORMAL.value;

    return (
      <div className={`baby-love-admin-tenant-list-item ${!isTenantNormal ? 'baby-love-common-boder-1px-red' : ''}`}>
        <div className="baby-love-admin-tenant-list-item-main">
          <div className="baby-love-admin-tenant-list-item-body">
            <div className="baby-love-admin-tenant-list-item-body-label">
              租户名：
            </div>
            <div className="baby-love-admin-tenant-list-item-body-content">
              {tenantItem.tenantName || "--"}
            </div>
          </div>
          <div className="baby-love-admin-tenant-list-item-body">
            <div className="baby-love-admin-tenant-list-item-body-label">
              租户编码：
            </div>
            <div className="baby-love-admin-tenant-list-item-body-content">
            {tenantItem.tenantCode || "--"}
            </div>
          </div>
          <div className="baby-love-admin-tenant-list-item-body">
            <div className="baby-love-admin-tenant-list-item-body-label">
              创建时间：
            </div>
            <div className="baby-love-admin-tenant-list-item-body-content">
              {tenantItem.createdAt || "--"}
            </div>
          </div>
          <div className="baby-love-admin-tenant-list-item-body">
            <div className="baby-love-admin-tenant-list-item-body-label">
              租户状态：
            </div>
            <div className={`baby-love-admin-tenant-list-item-body-content ${!isTenantNormal ? 'baby-love-common-font-red' : ''}`}>
              {tenantItem.tenantStatusText || "--"}
            </div>
          </div>
        </div>
        <div
          className="baby-love-admin-tenant-list-item-footer"
          onClick={stopPropagation}
        >
          <div className="baby-love-admin-tenant-list-item-footer-created-time">
            {tenantItem.createdAt}
          </div>
          <div className="baby-love-admin-tenant-list-item-footer-actions">
            <Button color="primary" fill="none" onClick={this.updateTenant}>
              修改
            </Button>
            <Button color="primary" fill="none" onClick={this.toUserList}>
              用户
            </Button>
            {tenantItem.isAdmin === '是' ? null : (
              <Button color="primary" fill="none" onClick={this.removeTenant}>
                删除
              </Button>
            )}
          </div>
        </div>
      </div>
    );
  }
}

export default GoodsItem;
